<template>
  <div class="container">
    <div class="cron">
      <h1>登录</h1>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="70px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="button">
          <el-button type="primary" @click="submitForm('ruleForm')" size="small"
            >登陆</el-button
          >
          <el-button @click="resetForm('ruleForm')" size="small"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          {
            min: 3,
            max: 45,
            message: "长度在 3 到 45 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        const user = {
          username:this.ruleForm.username,
          password:this.ruleForm.password
        }
        if (valid) {
          this.$store.dispatch('Login', user).then(() => {
            this.$message({
              message:'登录成功',
              type:'success'
            })
            this.$router.push( {name:'/index'} )
          }).catch(() => {
            return false;
          })
        } else {
          this.$message.error("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang='scss' scoped>
@import "@/assets/style/style.scss";
.container {
  width: 100%;
  height: 100%;
  background-color: #08a9b4;
  position: relative;
}
.cron {
  h1 {
    font-size: 25px;
    margin: 0 auto 20px;
  }
  width: 300px;
  height: 230px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10px;
  box-shadow: 0 0 15px #979797;
  background-color: white;
  padding: 20px 30px 20px 20px;
  margin: 0 auto;
  .button {
    text-align: left;
  }
}
</style>